#include "Sprite.h"
#include "SpriteSheet.h"
#include "Timer.h"

Sprite::Sprite(SpriteSheet* spriteSheet)
{
	m_spriteSheet = spriteSheet;

	//making a bouncing mechanic
	m_acc = Vec2f(0, 100);
	m_vel = Vec2f(70, 0);

	m_cellNum = 0;
	m_elapsedTime = 0;
}

Sprite::~Sprite()
{

}

void Sprite::Draw()
{
	m_spriteSheet->Draw((int)m_pos.x, (int)m_pos.y, m_cellNum, Screen::Instance());
}

void Sprite::Update()
{
	float dt = TheTimer::Instance()->GetDT();

	// new vel = old vel + acc * time
	Vec2f oldVel = m_vel;
	m_vel += m_acc * dt;
	m_pos += (oldVel + m_vel) * 0.5 * dt;

	//boundry checking
	if (m_pos.y > 380)
	{
		m_vel.y = -m_vel.y;
	}
	if (m_pos.x > 530)
	{
		m_vel.x = -m_vel.x;
	}
	if (m_pos.x < 1)
	{
		m_vel.x = -m_vel.x;
	}

	//TODO: make elapsedTime and cellNum data driven 
	//(ie. making the hard coded numbers into variables)
	m_elapsedTime += dt;
	if (m_elapsedTime > 0.1f)
	{
		m_elapsedTime = 0;
		m_cellNum++;
		if (m_cellNum > 15)
		{
			m_cellNum = 0;
		}
	}
}
